#include"jiajia_lmcc.h"

extern void JiaJia_word_meminit(void *pt, size_t size){
    JiaJia_word_pt jjword = (JiaJia_word_pt)pt;
    register size_t i = 0;

    size /= sizeof(JiaJia_word_t);
    for(; i < size; ++i){
        _JiaJia_word_meminit_once(&jjword[i]);
    }
}

extern void JiaJia_word_memfree(void *pt, size_t size){
    JiaJia_word_pt jjword = (JiaJia_word_t)pt;
    register size_t i = 0;

    size /= sizeof(JiaJia_word_t);
    for(; i < size; ++i){
        Yu_string_free(&(jjword[i].text));
        _JiaJia_word_meminit_once(pt, size);
    }
}

extern void JiaJia_word_queue_meminit(void *pt, size_t size){
    JiaJia_word_queue_pt queue = (JiaJia_word_queue_pt)pt;
    register size_t i = 0;

    size /= sizeof(JiaJia_word_queue_t);
    for(; i < size; ++i){
        _JiaJia_word_queue_meminit_once(queue);
    }
}

extern void JiaJia_word_queue_memfree(void *pt, size_t size){
    JiaJia_word_queue_pt queu = (JiaJia_word_queue_pt)pt;
    register size_t i = 0;
    register uint32_t next = 0;

    size /= sizeof(JiaJia_word_queue_t);
    for(; i < size; ++i){
        next = queue[i].head;
        while(next != queue->tail){
            JiaJia_word_free(&(queue[i].buf[next]));
            next = (next < (JIAJIA_WORD_QUEUE_SIZE - 1))? (next + 1):(0);
        }

        _JiaJia_word_queue_meminit_once(&queue[i]);
    }
}

extern int16_t JiaJia_word_accept(int16_t encode){
    JiaJia_file_pt jjfile = JiaJia_file_top();
    JiaJia_word_t *jjword = NULL;

    Yu_stdlog("%s\n", JiaJiaLmcc_encode_str(encode));

    /* 无视 blank 字 */
    if(encode == _JiaJiaLmcc_word_blank)
        return encode;

    /* 整合 id 字 */
    if(jjfile->word_end != NULL &&
       jjfile->word_end->word == _JiaJiaLmcc_word_id &&
       encode == _JiaJiaLmcc_word_id){
        JiaJia_throw_string(&jjfile->word_end->text);
        Yu_print_fmt("%s\n", &jjfile->word_end->text);
        return encode;
    }

    /* 分配新的字 */
    jjword = JiaJia_word_alloc();
    JiaJia_word_fileset(jjword, encode);

    Yu_print_fmt("%s\n", &jjword->text);

    /* 加入 jjfile 的 word 队列 */
    if(jjfile->word_end == NULL){
        jjfile->word_begin = jjword;
        jjfile->word_next = jjword;
        jjfile->word_reduce = jjword;
    }else{
        jjfile->word_end->next = jjword;
    }

    jjfile->word_end = jjword;

    return encode;
}

extern int16_t JiaJia_next_word(){
    JiaJia_file_pt jjfile = JiaJia_file_top();
    JiaJia_word_t *jjword = NULL;

    int16_t word = _JiaJiaLmcc_word_eof;

    if(jjfile->word_next == NULL){
        while(1){
            word = JiaJiaLmcc_next_word();

            if(word == _JiaJiaLmcc_word_dot ||       /* 。 */
               word == _JiaJiaLmcc_word_semicolon || /* ； */
               word == _JiaJiaLmcc_word_eof ||       /* EOF */
               word == _JiaJiaLmcc_word_err)         /* 出错 */
                break;
        }
    }

    if(jjfile->word_next != NULL){
        word = jjfile->word_next->word;

        /* 到了文件尾不变，出错不变。 */
        if(word != _JiaJiaLmcc_word_eof &&
           word != _JiaJiaLmcc_word_err)
            jjfile->word_next = jjfile->word_next->next;
    }

    return word;
}
